
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 用户管理
 * @author lxn
 */
class User {
	private static $data = array();
	public function __construct()
	{
		parent::__construct();
		$this->load->model('User_model','user_model');	
		$this->load->model('Type_model','type_model');	
	}

    /**
     *@title 微信小程序登录接口
     *@desc 微信小程序登录接口
     *@input {"name":"code","require":"true","type":"int","desc":"这个code是用wx.login接口获取到的code"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     *
     *@output {"name":"data.userId","type":"int","desc":"登录用户的ID,小程序端自己维护用户登录态,若有其他要求,再商量"}
     * */
    public function weixin_login()
    {
        $code=$this->input->post('code');
        if (!$code){
            show300('参数错误');
        }
        
        $appid = APPId;     //小程序ID
        $appsecret = APPSECRET;     //小程序密钥
        $wx_url='https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code='.$code.'&grant_type=authorization_code';
        $res=$this->curl_request($wx_url);
        
        $data=json_decode($res);
        $openid=@$data->openid;
        $userId=$this->getId($openid);
        if ($userId){            
            $resopne=['userId'=>$userId];
            show200($resopne);
        }else{
            show400('获取用户数据失败');
        }
    }
    
    /**
     *@title 应聘者列表
     *@desc 应聘者列表(获取所有在找工作的用户列表)
     *
     *@input {"name":"workCityId","type":"int","desc":"应聘者希望的工作城市ID,如果不传则显示所有地区,即全国"}
     *@input {"name":"search","type":"int","desc":"查询应聘用户名称"}
     *@input {"name":"jobId","type":"int","desc":"按职位ID筛选,不筛选则可不传"}
     *
     *@input {"name":"page","type":"int","desc":"职位分页数,从0开始","default":"0"}
     *@input {"name":"limit","type":"int","desc":"一页显示的条数","default":"20"}
     *
     *@output {"name":"code","type":"int","desc":"200:获取成功,400获取失败,无数据"}
     *@output {"name":"msg","type":"string","desc":"获取成功/获取失败"}
     *
     *@output {"name":"data","type":"array","desc":"用户列表,下面是详细说明,index代表该数组下标"}
     *@output {"name":"data[index].userId","desc":"用户Id","child":"1"}
     *@output {"name":"data[index].avatar","desc":"用户头像","child":"1"}
     *@output {"name":"data[index].birthday","desc":"用户生日,如果需要年龄,可以前端自己转换","child":"1"}
     *@output {"name":"data[index].exp","desc":"用户工作经验","child":"1"}
     *@output {"name":"data[index].jobName","desc":"期望的职位名称","child":"1"}
     *@output {"name":"data[index].realName","desc":"用户姓名","child":"1"}
     *@output {"name":"data[index].salaryName","desc":"期望的薪资名称","child":"1"}
     * */
    public function applyList()
    {    
        $workCityId=$this->input->post('workCityId');       //工作城市ID
        $search=$this->input->post('search');   //搜索
        $order=$this->input->post('order');     //排序,默认不传为推荐排序,传此参数则意为最新排序
        $jobId=$this->input->post('jobId');
        
        $page=$this->input->post('page');
        $limit=$this->input->post('limit');
    
        $page=!empty($page)?$page:0;
        $limit=!empty($limit)?$limit:20;
        $where=[];  //筛选条件数组
        //以下是为了组装查询条件数组
        if($jobId){
            $where['user.jobId']=$jobId;
        }
        if($workCityId){
            $where['user.workCityId']=$workCityId;
        }
        
        
        $offset=$page*$limit;        //数据库起始偏移量
        $select='user.userId,';
        $job=$this->user_model->getApplyList($offset,$limit,$search,$where,$order);
    
        if(!$job){
            show400();
        }
        
        show200($job);
    }
    
    /**
     *@title 获取用户信息
     *@desc 获取用户信息(此接口显示了该程序内所有用到的用户信息,在需要的时候统一调用即可,不再做其他专用接口)
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     *
     *@output {"name":"data.userId","type":"int","desc":"登录用户的ID,小程序端自己维护用户登录态,若有其他要求,再商量"}
     *@output {"name":"data.realName","type":"string","desc":"真实姓名"}
     *@output {"name":"data.sex","type":"string","desc":"用户的性别，值为1时是男性，值为2时是女性，值为0时是未知"}
     *@output {"name":"data.birthday","type":"string","desc":"生日,用户年龄那一栏建议改成用户生日,因为年龄会一直变"}
     *@output {"name":"data.workCityId","type":"string","desc":"工作城市ID"}
     *@output {"name":"data.cityName","type":"string","desc":"工作城市名称"}
     *@output {"name":"data.jobId","type":"string","desc":"期望职业分类ID"}
     *@output {"name":"data.jobName","type":"string","desc":"期望职业分类名称"}
     *@output {"name":"data.salaryId","type":"string","desc":"期望薪资分类ID"}
     *@output {"name":"data.salaryName","type":"string","desc":"期望薪资分类名称"}
     *@output {"name":"data.workStatus","type":"string","desc":"当前工作状态:1找工作 2不找工作"}
     *@output {"name":"data.salaryName","type":"string","desc":"当前工作状态分类名称"}
     *@output {"name":"data.exp","type":"string","desc":"工作经验"}
     *@output {"name":"data.intro","type":"string","desc":"个人介绍"}
     *@output {"name":"data.skill","type":"array","desc":"个人职业技能数据,数组形式"}
     *@output {"name":"data.skill[index].typeId","type":"string","desc":"技能分类ID","child":"1"}
     *@output {"name":"data.skill[index].typeName","type":"string","desc":"技能分类名称","child":"1"}
     * */
    public function getUserinfo(){
        $userId=$this->input->post('userId');
        if(!$userId){
            show3();
        }
        
        $userinfo=$this->user_model->getUserRow($userId);
        if(!$userinfo){
            show400();
        }
        if($userinfo['skill']){
            $skills=explode('|', $userinfo['skill']);
            $skillsdata=$this->type_model->getList($skills);
            $userinfo['skill']=$skillsdata;
        }else{
            $userinfo['skill']=[];
        }
        show200($userinfo);
    }
    /**
     *@title 完善个人信息
     *@desc 完善个人信息
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"realName","require":"true","type":"string","desc":"真实姓名"}
     *@input {"name":"phone","require":"true","type":"int","desc":"手机号"}
     *@input {"name":"intro","require":"true","type":"int","desc":"个人介绍"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function updateUserinfo(){
        $userId=$this->input->post('userId');        
        $intro=$this->input->post('intro');
        $realName=$this->input->post('realName');
        $phone=$this->input->post('phone');
        if(!$intro||!$realName||!$phone){
            show300('必填参数不能为空');
        }
        if(!$userId){
            show3();
        }
        $data=[
            'intro'=>$intro,
            'realName'=>$realName,
            'phone'=>$phone
        ];
        $res=$this->user_model->update($userId,$data);
        if ($res){
            show200('修改成功');
        }
        else{
            show400('修改失败');
        }                
    }
    
    /**
     *@title 修改简历中的基本资料
     *@desc 修改简历中的基本资料
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"realName","require":"true","type":"string","desc":"真实姓名"}
     *@input {"name":"phone","require":"true","type":"strinig","desc":"手机号"}
     *@input {"name":"intro","require":"true","type":"strinig","desc":"个人介绍"}
     *@input {"name":"birthday","require":"true","type":"strinig","desc":"出生日期"}
     *@input {"name":"exp","require":"true","type":"strinig","desc":"工作时间(工作经验)"}
     *@input {"name":"sex","require":"true","type":"strinig","desc":"性别:1男,2女,0未知"}    
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function updateResumeInfo(){
        $userId=$this->input->post('userId');
        $intro=$this->input->post('intro');
        $realName=$this->input->post('realName');
        $phone=$this->input->post('phone');
        $birthday=$this->input->post('birthday');
        $exp=$this->input->post('exp');
        $sex=$this->input->post('sex');
        if(!$intro||!$realName||!$phone||!$birthday||!$exp||!$sex){
            show300('必填参数不能为空');
        }
        if(!$userId){
            show3();
        }
        $data=[
            'intro'=>$intro,
            'realName'=>$realName,
            'phone'=>$phone,
            'birthday'=>$birthday,
            'exp'=>$exp,
            'sex'=>$sex,
        ];
        $res=$this->user_model->update($userId,$data);
        if ($res){
            show200('修改成功');
        }
        else{
            show400('修改失败');
        }
    }
    
    /**
     *@title 修改简历中的求职意向
     *@desc 修改简历中的求职意向
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"jobId","require":"true","type":"string","desc":"期望职位ID"}
     *@input {"name":"salaryId","require":"true","type":"int","desc":"期望薪资ID"}
     *@input {"name":"workCityId","require":"true","type":"int","desc":"工作城市ID"}
     *@input {"name":"workStatus","require":"true","type":"int","desc":"当前工作状态:1找工作,2不找工作"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function updateResumeIntent(){
        $userId=$this->input->post('userId');
        $jobId=$this->input->post('jobId');
        $salaryId=$this->input->post('salaryId');
        $workCityId=$this->input->post('workCityId');
        $workStatus=$this->input->post('workStatus');
        if(!$jobId||!$salaryId||!$workCityId||!$workStatus){
            show300('必填参数不能为空');
        }
        if(!$userId){
            show3();
        }
        $data=[
            'jobId'=>$jobId,
            'salaryId'=>$salaryId,
            'workCityId'=>$workCityId,
            'workStatus'=>$workStatus,
        ];
        $res=$this->user_model->update($userId,$data);
        if ($res){
            show200('修改成功');
        }
        else{
            show400('修改失败');
        }
    }
    
    /**
     *@title 修改简历中的职业技能
     *@desc 修改简历中的职业技能
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"skill","require":"true","type":"string","desc":"拥有的技能ID,JS中该参数写成数组形式,则会自动转换参数格式,如skill=[1,2,3]"}     
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function updateResumeSkill(){
        $userId=$this->input->post('userId');
        $skill=$this->input->post('skill');
        
        if(!$skill){
            show300('技能ID参数不正确');
        }
        
        if(!$userId){
            show3();
        }
        $skillStrig=str_replace(',','|', $skill);
        $data=[
            'skill'=>$skillStrig,           
        ];
        $res=$this->user_model->update($userId,$data);
        if ($res){
            show200('修改成功');
        }
        else{
            show400('修改失败');
        }
    }
    
    /**
     *@title 获取用户手机号
     *@desc 获取用户手机号
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300:用户未设置手机号"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     *
     *@output {"name":"data.userId","type":"int","desc":"用户ID"}
     *@output {"name":"data.phone","type":"string","desc":"用户手机号"}
     * */
    public function getPhone(){
        $userId=$this->input->post('userId');
        if(!$userId){
            show3();
        }
        $select='userId,phone';
        $userinfo=$this->user_model->getUserRow($userId,$select);
        if(!$userinfo){
            show400('没有该用户信息');
        }
        if(!$userinfo['phone']) show300('还未设置手机号');
        $userinfo['phone']=substr_replace($userinfo['phone'], '****', 3,4);
        show200($userinfo);
    }
    
    /**
     *@title 绑定手机号
     *@desc 绑定手机号
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function bindPhone(){
        $userId=$this->input->post('userId');
        $phone=$this->input->post('phone');
        
        if(!$userId){
            show3();
        }
        if(!$phone){
            show300('手机号不能为空');
        }
        $res=$this->user_model->update($userId,['phone'=>$phone]);
        if ($res){
            show200('修改成功');
        }
        else{
            show400('修改失败');
        }
    }
    
    /**
     *@title 检测手机号输入是否正确
     *@desc 检测手机号输入是否正确(用于用户更改手机号时输入旧手机号是否正确的判断)
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function checkPhone(){
        $userId=$this->input->post('userId');
        $phone=$this->input->post('phone');
        
        if(!$userId){
            show3();
        }
        if(!$phone){
            show300('手机号不能为空');
        }
        $select='userId,phone';
        $userinfo=$this->user_model->getUserRow($userId,$select);
        if(@$userinfo['phone']==$phone){
            show200('手机号输入正确');
        }else{
            show400('手机号输入不正确');
        }
    }
    /**
     *@title 发送验证码
     *@desc 发送手机验证码
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function sendSms(){
        $phone=$this->input->post('phone');
        if(!$phone){
            //show300('手机号不能为空');
        }
        // 需要发送短信的手机号码
        $phoneNumbers = [$phone];
        
        
        // 短信模板ID，需要在短信应用中申请
        $templateId= 7839;  // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请
        
        // 签名
        $smsSign = "腾讯云"; // NOTE: 这里的签名只是示例，请使用真实的已申请的签名，签名参数使用的是`签名内容`，而不是`签名ID`
        $yzm=rand(1000,9999);
        $params=[$yzm];     //模板里的参数,此处即手机验证码
        $config=['smsSign'=>$smsSign,'templateId'=>$templateId,'phoneNumbers'=>$phoneNumbers,'params'=>$params];
        $this->load->library('sms/sendsms',$config);
        $res=$this->sendsms->do_send();
        $this->session->set_userdata('yzm', $yzm);
        
        echo session_id();          //sessionid,因为小程序在请求头中并没有cookie,所以要想在小程序中依然用session的话必须请求头中加入cookie参数,如'Cookie': 'ci_session=4vd6svd57d5e25pfjg3ntp3k798d00rk'
    }
    
    /**
     *@title 检测验证码是否正确
     *@desc 检测验证码是否正确
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *@input {"name":"zym","require":"true","type":"string","desc":"输入的手机验证码"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function checkSms(){
        $phone=$this->input->post('phone');
        $yzm=$this->input->post('yzm');
        if(!$phone){
            show300('手机号不能为空');
        }
        if(!$yzm){
            show300('验证码不能为空');
        }
        if($yzm==$this->session->userdata('yzm')){
            show200([],'验证码输入正确');
        }else{
            show400('验证码输入错误');
        }
    }
    
    //如果未注册则自动注册并返回用户ID,如果已注册则直接返回用户ID
    private function getId($openid){
        $user=$this->User_model->is_reg($openid);
        if($user&&$user['userId']){ //已注册
            return $user['userId'];
        }else{
            $userId=$this->User_model->insertData(['wxId'=>$openid]);
            return $userId;
        }
    }
    
    //curl
    private function curl_request($url,$timeout=30,$header=array()){
        if (!function_exists('curl_init')) {
            throw new Exception('server not install curl');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $data = curl_exec($ch);
        @curl_close($ch);
        return $data;exit();
        
    }
    
    
	
	


}
/* End of file Login.php */